import 'package:hive/hive.dart';

part 'expense_model.g.dart';

/// 记账类型枚举
@HiveType(typeId: 1)
enum ExpenseType {
  @HiveField(0)
  income, // 收入
  @HiveField(1)
  expense, // 支出
}

/// 记账分类枚举
@HiveType(typeId: 2)
enum ExpenseCategory {
  // 收入分类
  @HiveField(0)
  salary, // 工资
  @HiveField(1)
  bonus, // 奖金
  @HiveField(2)
  investment, // 投资收益
  @HiveField(3)
  partTime, // 兼职
  @HiveField(4)
  gift, // 礼金
  @HiveField(5)
  other, // 其他收入
  // 支出分类
  @HiveField(10)
  food, // 餐饮
  @HiveField(11)
  transport, // 交通
  @HiveField(12)
  shopping, // 购物
  @HiveField(13)
  entertainment, // 娱乐
  @HiveField(14)
  healthcare, // 医疗
  @HiveField(15)
  education, // 教育
  @HiveField(16)
  housing, // 住房
  @HiveField(17)
  utilities, // 水电费
  @HiveField(18)
  insurance, // 保险
  @HiveField(19)
  travel, // 旅行
  @HiveField(20)
  pets, // 宠物
  @HiveField(21)
  charity, // 慈善
  @HiveField(22)
  otherExpense, // 其他支出
}

/// 记账条目模型
@HiveType(typeId: 4)
class ExpenseModel extends HiveObject {
  @HiveField(0)
  String id;

  @HiveField(1)
  ExpenseType type; // 类型：收入或支出

  @HiveField(2)
  double amount; // 金额

  @HiveField(3)
  ExpenseCategory category; // 分类

  @HiveField(4)
  String description; // 描述/备注

  @HiveField(5)
  DateTime date; // 记账日期

  @HiveField(6)
  DateTime createdAt; // 创建时间

  @HiveField(7)
  DateTime updatedAt; // 更新时间

  ExpenseModel({
    required this.id,
    required this.type,
    required this.amount,
    required this.category,
    required this.description,
    required this.date,
    required this.createdAt,
    required this.updatedAt,
  });

  /// 创建新的记账条目
  factory ExpenseModel.create({
    required ExpenseType type,
    required double amount,
    required ExpenseCategory category,
    required String description,
    DateTime? date,
  }) {
    final now = DateTime.now();
    return ExpenseModel(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      type: type,
      amount: amount,
      category: category,
      description: description,
      date: date ?? now,
      createdAt: now,
      updatedAt: now,
    );
  }

  /// 复制并更新记账条目
  ExpenseModel copyWith({
    String? id,
    ExpenseType? type,
    double? amount,
    ExpenseCategory? category,
    String? description,
    DateTime? date,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return ExpenseModel(
      id: id ?? this.id,
      type: type ?? this.type,
      amount: amount ?? this.amount,
      category: category ?? this.category,
      description: description ?? this.description,
      date: date ?? this.date,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? DateTime.now(),
    );
  }

  @override
  String toString() {
    return 'ExpenseModel(id: $id, type: $type, amount: $amount, category: $category, description: $description, date: $date)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ExpenseModel && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}

/// 扩展方法：获取分类的显示名称
extension ExpenseCategoryExtension on ExpenseCategory {
  String get displayName {
    switch (this) {
      // 收入分类
      case ExpenseCategory.salary:
        return '工资';
      case ExpenseCategory.bonus:
        return '奖金';
      case ExpenseCategory.investment:
        return '投资收益';
      case ExpenseCategory.partTime:
        return '兼职';
      case ExpenseCategory.gift:
        return '礼金';
      case ExpenseCategory.other:
        return '其他收入';

      // 支出分类
      case ExpenseCategory.food:
        return '餐饮';
      case ExpenseCategory.transport:
        return '交通';
      case ExpenseCategory.shopping:
        return '购物';
      case ExpenseCategory.entertainment:
        return '娱乐';
      case ExpenseCategory.healthcare:
        return '医疗';
      case ExpenseCategory.education:
        return '教育';
      case ExpenseCategory.housing:
        return '住房';
      case ExpenseCategory.utilities:
        return '水电费';
      case ExpenseCategory.insurance:
        return '保险';
      case ExpenseCategory.travel:
        return '旅行';
      case ExpenseCategory.pets:
        return '宠物';
      case ExpenseCategory.charity:
        return '慈善';
      case ExpenseCategory.otherExpense:
        return '其他支出';
    }
  }

  String get icon {
    switch (this) {
      // 收入分类
      case ExpenseCategory.salary:
        return '💰';
      case ExpenseCategory.bonus:
        return '🎁';
      case ExpenseCategory.investment:
        return '📈';
      case ExpenseCategory.partTime:
        return '💼';
      case ExpenseCategory.gift:
        return '🧧';
      case ExpenseCategory.other:
        return '💵';

      // 支出分类
      case ExpenseCategory.food:
        return '🍽️';
      case ExpenseCategory.transport:
        return '🚗';
      case ExpenseCategory.shopping:
        return '🛍️';
      case ExpenseCategory.entertainment:
        return '🎮';
      case ExpenseCategory.healthcare:
        return '🏥';
      case ExpenseCategory.education:
        return '📚';
      case ExpenseCategory.housing:
        return '🏠';
      case ExpenseCategory.utilities:
        return '💡';
      case ExpenseCategory.insurance:
        return '🛡️';
      case ExpenseCategory.travel:
        return '✈️';
      case ExpenseCategory.pets:
        return '🐕';
      case ExpenseCategory.charity:
        return '❤️';
      case ExpenseCategory.otherExpense:
        return '📝';
    }
  }

  /// 获取收入分类列表
  static List<ExpenseCategory> get incomeCategories => [
    ExpenseCategory.salary,
    ExpenseCategory.bonus,
    ExpenseCategory.investment,
    ExpenseCategory.partTime,
    ExpenseCategory.gift,
    ExpenseCategory.other,
  ];

  /// 获取支出分类列表
  static List<ExpenseCategory> get expenseCategories => [
    ExpenseCategory.food,
    ExpenseCategory.transport,
    ExpenseCategory.shopping,
    ExpenseCategory.entertainment,
    ExpenseCategory.healthcare,
    ExpenseCategory.education,
    ExpenseCategory.housing,
    ExpenseCategory.utilities,
    ExpenseCategory.insurance,
    ExpenseCategory.travel,
    ExpenseCategory.pets,
    ExpenseCategory.charity,
    ExpenseCategory.otherExpense,
  ];
}

/// 扩展方法：获取类型的显示名称
extension ExpenseTypeExtension on ExpenseType {
  String get displayName {
    switch (this) {
      case ExpenseType.income:
        return '收入';
      case ExpenseType.expense:
        return '支出';
    }
  }
}
